/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2018
 */

package com.bm.console.business.impl;
import com.alibaba.druid.util.StringUtils;
import com.aliyun.oss.OSSClient;
import com.bm.console.common.OSSConfig;
import com.sys.console.cookie.Constant_Cookie;
import com.sys.console.pojo.Sys_User;
import com.sys.object.common.page.PageBean;

import com.bm.console.business.WebCountryService;
import com.bm.console.pojo.WebCountry;
import com.bm.console.pojo.mapper.WebCountryMapper;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.sys.object.common.utils.Base64Helper;
import com.sys.object.common.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class WebCountryServiceImpl implements WebCountryService {

	@Autowired
	WebCountryMapper webCountryMapper;


	@Autowired
	private HttpServletRequest request;


	@Override
	public PageBean queryPageList(WebCountry entity, PageBean page) throws Exception {
		int rowCount = webCountryMapper.selectByPageCount(entity);
		page.setRowCount(rowCount);
		entity.setOffset(page.getStartRow());
		entity.setLimit(page.getPerpage());
		page.setList(webCountryMapper.selectByPage(entity));
		return page;
	}

	public WebCountry queryByPrimaryKey(Integer id) throws Exception {
		return webCountryMapper.selectByPrimaryKey(id);
	}

	public int saveOrUpdate(WebCountry entity) throws Exception {

		Sys_User currentUser = (Sys_User)request.getSession().getAttribute(Constant_Cookie.CURRENT_USER_SESSION);
		if (!StringUtils.isEmpty(entity.getFiledata()) && !StringUtils.isEmpty(entity.getFileext())) {
			byte[] data = Base64Helper.getImageFromBASE64ToByte(entity.getFiledata());
			String filename = UUID.randomUUID() + "." + entity.getFileext();

			OSSClient ossClient = new OSSClient(OSSConfig.oss_endpoint, OSSConfig.oss_accessKeyId,
					OSSConfig.oss_accessKeySecret);

			String eTag = OSSUtil.uploadObject2OSS(ossClient, data, filename, OSSConfig.oss_backetName, "photo/");
			ossClient.shutdown();
			if (!StringUtils.isEmpty(eTag)) {
				entity.setPhoto(OSSConfig.oss_domain + "photo/" + filename);
			}
		}

		if(entity.getId()!=null && entity.getId() > 0){
			entity.setUpdateId(Long.valueOf(currentUser.getUid()));
			entity.setUpdateTime(new Date());
			return this.webCountryMapper.updateByPrimaryKeySelective(entity);
		}

		entity.setCreateTime(new Date());
		entity.setCreateId(Long.valueOf(currentUser.getUid()));
		return this.webCountryMapper.insert(entity);
	}

	public int remove(Integer id) throws Exception {
		return this.webCountryMapper.deleteByPrimaryKey(id);
	}

	public int delete(Integer id) throws Exception {
		return this.webCountryMapper.deleteByPrimaryKey(id);
	}
	
	public int updateStatus(WebCountry entity) throws Exception{
		return this.webCountryMapper.updateByPrimaryKeySelective(entity);
	}

	public int updateStatus(String status, String[] ids)
			throws Exception {
		
		return 0;
	}


	@Override
	public List<WebCountry> selectByAll() {
		return webCountryMapper.selectByAll();
	}
}
